﻿requirejs.config({
    paths: {
    	'text': '../lib/require/text',
        'durandal':'../lib/durandal/js',
        'plugins' : '../lib/durandal/js/plugins',
        'transitions' : '../lib/durandal/js/transitions',
        'q': '../lib/q/q',
        'moment': '../lib/moment/moment.min',
        'toastr': '../lib/toastr/js/toastr',
        'i18n': '../lib/i18n/i18next.amd.withJQuery-1.7.3',
        'pdfjs': '../lib/pdfjs/pdf',
        'pdfjsIE9': '../lib/pdfjs/compatibility'
    },
    shim: {
    	'pdfjs': {
            deps: ['pdfjsIE9'],
            init: function () {
                this.PDFJS.workerSrc = require.toUrl('../lib/pdfjs/pdf.worker.js');
            }
        }
    }
});

define('knockout', function () {return ko;});
define('jquery', function () {return $;});
define('underscore', function () {return _;});

define(['durandal/system', 
        'durandal/app', 
        'durandal/viewLocator',
        'durandal/binder',
        'services/config',
        'services/appConfig',
        'services/user',
        'i18n',
        'toastr',
        'plugins/dialog',
        'common/bs3DialogContext'],  
    function (system, app, viewLocator, binder, config, appConfig, user, i18n, toastr, dialog, bs3Dialog) {
	
    //>>excludeStart("build", true);
    system.debug(true);
    //>>excludeEnd("build");

    /* Configure application informations*/
    app.title = config.app.name;
    toastr.options = config.toastr;

    app.configurePlugins({
        router:true,
        dialog: true,
        widget: {
			kinds: ['actions', 'formTable', 'formEditTable', 'page', 'formLeftMenu', 'formEditButton']
		}
    });
    
    /**
     * Adds a new function to i18n.
     * 'ts()' is a shortcut to call t() with sprintf parameters (replaces %s occurences with the given parameter)
     * usage: i18n.ts('cms:key', param1, param2, param3, ...)
     * @param key
     * @returns {*}
     */
    i18n.ts = function (key) {
        return i18n.t.call(i18n, key, {
            postProcess: 'sprintf',
            sprintf: Array.prototype.slice.call(arguments, 1)
        });
    };

    app.start().then(function() {
    	// registers new Dialog context to use BS3's dialog
        dialog.addContext("BS3Dialog", bs3Dialog);
        
		appConfig.loadConfigAndUser()
		.then(function (configData) {
			user.initial(configData.user);
            return user;})
		.then(function (userData) {
			// set i18n depending on user pref.
            config.i18nOptions.lng = userData.locale() || 'en';
            
			i18n.init(config.i18nOptions, function () {
				binder.binding = function (obj, view) {
		            $(view).i18n();
		        };

				// configure knockout bindings
				require([
					'bindings/formattedValue',
					'bindings/link',
					'bindings/security',
					'bindings/validation',
					'bindings/relayout',
					'bindings/enterKeyPressed',
					'bindings/formSectionTitles',
					'bindings/upload'
				], function (
					format,
					link,
					security,
					validators,
					relayout,
					enterKeyPressed,
					formSectionTitles,
					upload) {

					ko.bindingHandlers.formattedValue = format;
					ko.bindingHandlers.link = link;
					ko.bindingHandlers.security = security;
					ko.bindingHandlers.relayout = relayout;
					ko.bindingHandlers.enterKeyPressed = enterKeyPressed;
					ko.bindingHandlers.formSectionTitles = formSectionTitles;
					ko.bindingHandlers.upload = upload;

					_.each(validators.bindingsToActivate, function (bindingName) {
						ko.bindingHandlers[bindingName] = validators[bindingName];
					});
				});
				
				//Replace 'viewmodels' in the moduleId with 'views' to locate the view.
				//Look for partial views in a 'views' folder in the root.
				viewLocator.useConvention();

				//Show the app by setting the root view model for our application.
				app.setRoot('shell', 'entrance');
			});
		});
    });
});